class Solution {
    public:
        vector<long long> mostFrequentIDs(vector<int>& nums, vector<int>& freq) {
            vector<long long> ans(nums.size(), 0);
            long long cnt[100001] = {0};
            multiset<long long> ms;
            multiset<long long>::iterator it;
            for (int i = 0; i < nums.size(); i++) {
                long long& c = cnt[nums[i]];
                it = ms.find(c);
                if (it != ms.end()) {
                    ms.erase(it);
                }
                c += freq[i];
                ms.insert(c);
                ans[i] += *ms.rbegin();
            }
            return ans;
        }
    };